package com.loubii.sunshineSchool.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.loubii.sunshineSchool.R;
import com.loubii.sunshineSchool.pojo.ChatItem;
import com.loubii.sunshineSchool.ui.avtivity.SSChatActivity;
import com.loubii.sunshineSchool.util.ImageUtil;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerviewViewHolder;
import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;

public class ChatAdapter extends UltimateViewAdapter {

    private List<ChatItem> itemList = new ArrayList<>();
    private Context context;

    public void setData(List<ChatItem> itemList) {
        HashSet<Integer> set=new HashSet<>();
        for(int i=0;i<itemList.size();){
            ChatItem item=itemList.get(i);
            if(set.contains(item.getUserId())){
                itemList.remove(item);
            }else {
                set.add(item.getUserId());
                i++;
            }
        }
        this.itemList = itemList;
        java.util.Collections.sort(this.itemList, new Comparator<ChatItem>() {
            @Override
            public int compare(ChatItem o1, ChatItem o2) {
                return o2.getLatestTime().compareTo(o1.getLatestTime());
            }
        });
        notifyDataSetChanged();
    }

    public ChatAdapter(Context context) {
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder newFooterHolder(View view) {
        return new UltimateRecyclerviewViewHolder<>(view);
    }

    @Override
    public RecyclerView.ViewHolder newHeaderHolder(View view) {
        return new UltimateRecyclerviewViewHolder<>(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_bill_list, parent, false);
        ItemHoleder vh = new ItemHoleder(v);
        return vh;
    }

    @Override
    public int getAdapterItemCount() {
        return itemList.size();
    }

    @Override
    public long generateHeaderId(int position) {
        SimpleDateFormat f = new SimpleDateFormat("yyyy 年 MM 月 dd 日");
        return f.format(itemList.get(position).getLatestTime()).hashCode();
    }


    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_stick_header, parent, false);
        return new HeaderHoleder(view);
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((ItemHoleder) holder).itemView.setTag(position);
        ItemHoleder itemHoleder = (ItemHoleder) holder;
        itemHoleder.tvClassify.setText(itemList.get(position).getUserName());
        ImageUtil.downloadAvatar(itemList.get(position).getImage(), itemHoleder.ivClassify);
        itemHoleder.tvClassifyDescribe.setText(itemList.get(position).getLatestText());
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {
        HeaderHoleder headerHoleder = (HeaderHoleder) holder;
        SimpleDateFormat f = new SimpleDateFormat("yyyy 年 MM 月 dd 日");
        headerHoleder.tvStickyDay.setText(f.format(itemList.get(position).getLatestTime()));
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }


    class ItemHoleder extends UltimateRecyclerviewViewHolder implements View.OnClickListener {

        ImageView ivClassify;
        TextView tvClassify;
        TextView tvClassifyDescribe;

        public ItemHoleder(View itemView) {
            super(itemView);
            ivClassify = (ImageView) itemView.findViewById(
                    R.id.iv_classify);
            tvClassify = (TextView) itemView.findViewById(R.id.tv_classify);
            tvClassifyDescribe = (TextView) itemView.findViewById(R.id.tv_classify_describe);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, SSChatActivity.class);
            intent.putExtra("name", tvClassify.getText());
            intent.putExtra("uid", itemList.get(getAdapterPosition()).getUserId());
            context.startActivity(intent);
        }
    }


    class HeaderHoleder extends UltimateRecyclerviewViewHolder {

        TextView tvStickyDay;

        public HeaderHoleder(View itemView) {
            super(itemView);
            tvStickyDay = (TextView) itemView.findViewById(R.id.tv_sticky_day);
        }

    }


}
